//对用户的操作

const UUID = require('uuid');

//对表操作实现
const {UserProxy } = require('../proxy'); 
const { UserFeedback } = require('../models');
//md5加密
const { md5 } = require('../utils');


module.exports = {
	//用户注册
	register: function(req, res, next){
		var nickname = req.body.nickname; //用户名
		var telephoto = req.body.telephoto; //手机号
		var password = req.body.password; //密码
		var sex = req.body.sex; //性别
		password = md5(password).toUpperCase();
		//console.log(nickname, telephoto, password)
		UserProxy.findByName(nickname)
		.then((data)=>{
			if(data && data.length>0){  //该用户名已经被注册
				throw {msg:'Has'}
			}else{
				return true;
			}
		})
		.then(()=>{
			UserProxy.createUser({
				nickname: nickname,
				telephoto: telephoto,
				password: password,
				sex: sex
			})
			.then(()=>{
				res.send({msg:'OK'}).end();
			});
		})
		.catch( ex=>{
			res.send(ex).end();
		});
	},
	
	//获取用户列表
	list: function(req, res, next){
		var nickname = req.query.nickname; //用户名
		UserProxy.findByName(nickname)
		.then((data)=>{
			res.send({msg:'OK',data:data}).end();
		});
	},

	//获取单个用户信息
	getPersonInfo: function(req, res, next){
		var BB_num = req.query.BB_num; //用户名BB号
		var nickname = req.query.nickname; //
		UserProxy.findByfilter({
			uuid: BB_num,
			nickname: nickname
		})
		.then((data)=>{
			res.send({msg:'OK',data:data}).end();
		});
	},
	
	//用户登录
	login: function(req, res, next){
		var nickname = req.body.nickname; //用户名
		var password = req.body.password; //密码
		password = md5(password).toUpperCase();
		UserProxy.findByName(nickname)
		.then((data)=>{
			if(data && data.length==0){
				res.send({msg:'NO'}).end();
			}else{
				if(password!=data[0].password){
					res.send({msg:'Err'}).end();
				}else{
					res.send({msg:'OK', data: data[0]}).end();
				}
			}
		});
	},
	
	//修改密码
	reset_password: function(req, res, next){
		var telephoto = req.body.telephoto; //手机号
		var password = req.body.password; //密码
		password = md5(password).toUpperCase();
		UserProxy.findByTelephoto(telephoto)
		.then((data)=>{
			if(data && data.telephoto){
				return data;
			}else{
				throw {msg:'NO'};
			}
		})
		.then((data)=>{
			return UserProxy.updateInfo(data.ID, {
				password: password
			});
		})
		.then(()=>{
			res.send({msg:' OK'}).end();
		})
		.catch(ex =>{
			res.send(ex).end();
		});
	},

	//修改个人信息
	modify_info: function(req, res, next){
		var nickname = req.body.nickname; //\
		var uuid = req.body.BB_num; //
		var avatar = req.body.avatar; //
		var sex = req.body.sex; //
		var age = req.body.age; //
		UserProxy.findByfilter({
			nickname: nickname,
			uuid: uuid
		})
		.then((data)=>{
			if(data && data.nickname){
				return data;
			}else{
				throw {msg:'NO'};
			}
		})
		.then((data)=>{
			var origin_avatar = data.avatar;
			return UserProxy.updateInfo(data.ID, {
				nickname,
				uuid,
				avatar: avatar? avatar: origin_avatar,
				sex,
				age
			});
		})
		.then(()=>{
			res.send({msg:' OK'}).end();
		})
		.catch(ex =>{
			res.send(ex).end();
		});
	},
		
	//修改密码
	modify_password: function(req, res, next){
		var nickname = req.body.nickname; //
		var password = req.body.origin_pass; //密码
		var new_password = req.body.new_pass; //新密码
		password = md5(password).toUpperCase();
		new_password = md5(new_password).toUpperCase();
		
		UserProxy.findByfilter({
			nickname: nickname,
			password: password
		})
		.then((data)=>{
			if(data && data.nickname){
				return data;
			}else{
				throw {msg:'NO'};
			}
		})
		.then((data)=>{
			return UserProxy.updateInfo(data.ID, {
				password: password
			});
		})
		.then(()=>{
			res.send({msg:' OK'}).end();
		})
		.catch(ex =>{
			res.send(ex).end();
		});
	},
	
	//修改绑定手机号
	change_telephone: function(req, res, next){
		var nickname = req.body.nickname; //
		var telephone = req.body.telephone; //原手机号
		var new_telephone = req.body.new_telephone; //新手机号
		
		UserProxy.findByfilter({
			nickname: nickname,
			telephone: telephone
		})
		.then((data)=>{
			if(data && data.nickname){
				return data;
			}else{
				throw {msg:'NO'};
			}
		})
		.then((data)=>{
			return UserProxy.updateInfo(data.ID, {
				telephone: telephone
			});
		})
		.then(()=>{
			res.send({msg:' OK'}).end();
		})
		.catch(ex =>{
			res.send(ex).end();
		});
	},

	//用户反馈意见
	feedback_suggestion: function(req, res, next){
		var nickname = req.body.nickname; //
		var uuid = req.body.uuid; 
		var suggestion = req.body.suggestion; //意见
		UserProxy.findByfilter({
			nickname: nickname,
			uuid: uuid
		})
		.then((data)=>{
			if(data && data.nickname){
				return data;
			}else{
				throw {msg:'NO'};
			}
		})
		.then((data)=>{
			var uuid = UUID.v1().replace(/-/g,'').toUpperCase();
			return UserFeedback.create({
				ID: uuid,
				nickname: nickname,
				suggestion: suggestion
			});
		})
		.then(()=>{
			res.send({msg:'OK'}).end();
		})
		.catch(ex =>{
			res.send(ex).end();
		});
	}
	
}



